<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Present;
use App\Models\Oncustomer;
use App\Http\Requests\StoreCoupon;
use App\Http\Requests\UpdateCoupon;
use App\Models\Coupon;
use App\Models\CouponType;
use App\Models\Store;
use App\Models\Type;


class PresentController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_customers' => 'am-in',
            '_present' => 'am-active',
        ]);

    }

    //显示赠送优惠券页面
    public function present($id){
        $userInfo=Oncustomer::find($id);
        $couponArr=Coupon::with('store')->with('types')->get();
        return view('admin.present.present',compact('userInfo','couponArr'));
    }

    //处理赠送优惠券
    public function checkPresent(Request $request){
        $oncustomer_id=$request->oncustomer_id;
        $check=Present::where([['oncustomer_id',$oncustomer_id],['coupon_id',$request->coupon_id]])->first();
        if($check){
            $update=Present::where([['oncustomer_id',$oncustomer_id],['coupon_id',$request->coupon_id]])->update(['coupon_num'=>$check['coupon_num']+$request->coupon_num]);
            if($update){
                return redirect("admin/present/$oncustomer_id")->with('success', '赠送成功');
            }
        }else{
            $present=Present::create(['oncustomer_id'=>$oncustomer_id,'coupon_id'=>$request->coupon_id,'coupon_num'=>$request->coupon_num]);
            if($present){
                return redirect("admin/present/$oncustomer_id")->with('success', '赠送成功');
            }
        }


    }
}
